import 'package:color_dart/RgbaColor.dart';

/// first_page.dart
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/jsonserialize/im/contact.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/achievement_list.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/active_list.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/expert_list.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/need_list.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/recommend_list.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/serve_list.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import 'package:im_flutter_sdk_example/pages/chat_page.dart';
import 'package:im_flutter_sdk_example/provider/message_model.dart';
import 'package:im_flutter_sdk_example/utils/screen.dart';
import 'package:provider/provider.dart';

class _TabData {
  final Widget tab;
  final Widget body;
  _TabData({this.tab, this.body});
}

final _tabDataList = <_TabData>[
  _TabData(tab: Text('推荐'), body: RecommendList()),
  _TabData(tab: Text('项目'), body: AchievementList(isRecommend: false)),
  _TabData(tab: Text('活动'), body: ActiveList(isRecommend: false)),
  _TabData(tab: Text('需求'), body: NeedList(isRecommend: false)),
  _TabData(tab: Text('专家'), body: ExpertList(isRecommend: false)),
  _TabData(tab: Text('服务'), body: ServeList(isRecommend: false))
];

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  final tabBarList = _tabDataList.map((item) => item.tab).toList();
  final tabBarViewList = _tabDataList.map((item) => item.body).toList();

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _reqService();
    });
  }

  void _goChat() {
    // 到 联系人列表
    // G.pushNamed('/contact_list');
    chatToService();
  }

// 加载客服信息
  void _reqService() {
    EasyMobProvider easyMobProvider = Provider.of<EasyMobProvider>(context);
    easyMobProvider.reqContacts();
  }

  void chatToService() async {
    EasyMobProvider easyMobProvider = Provider.of<EasyMobProvider>(context);
    if (easyMobProvider.contact == null) return;
    Contact contact = easyMobProvider.contact;
    String chatter = contact.userId; // 约定 34 客服人员
    EMConversationType type = EMConversationType.Chat;
    try {
      EMConversation conversation =
          await EMClient.getInstance.chatManager.getConversation(chatter, type);
      // conversation
      conversation.ext = {"nickname": contact.username};
      Navigator.push(context,
          new MaterialPageRoute(builder: (BuildContext context) {
        return new ChatPage(conversation: conversation);
      }));
    } catch (e) {
      print('create conversation error');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DefaultTabController(
          length: tabBarList.length,
          child: Column(
            children: <Widget>[
              Container(
                width: double.infinity,
                height: 80,
                padding: EdgeInsets.fromLTRB(20, 24, 0, 0),
                alignment: Alignment.centerLeft,
                color: Colors.white,
                child: TabBar(
                    isScrollable: true,
                    indicatorColor: rgba(42, 125, 250, 0.44),
                    indicatorSize: TabBarIndicatorSize.label,
                    unselectedLabelColor: rgba(51, 51, 51, 1),
                    unselectedLabelStyle: TextStyle(fontSize: 32.nsp),
                    labelColor: rgba(51, 51, 51, 1),
                    labelStyle: TextStyle(
                        fontSize: 44.nsp, fontWeight: FontWeight.bold),
                    tabs: tabBarList),
              ),
              Expanded(
                  child: TabBarView(
                children: tabBarViewList,
                // physics: NeverScrollableScrollPhysics(), // 禁止滑动
              ))
            ],
          )),
      floatingActionButtonLocation: FloatingActionButtonLocation.miniEndFloat,
      floatingActionButton: GestureDetector(
        onTap: _goChat,
        child: SizedBox(
          child: Image.asset('lib/assets/images/float_contact.png'),
          width: 80,
          height: 110,
        ),
      ),
    );
  }
}
